package cn.yshujia.handler;

import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.enums.Info;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 认证失败处理器
 */

@Component
public class JwtAuthenticationHandler implements AuthenticationEntryPoint {
	
	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		ServletOutputStream outputStream = response.getOutputStream();
		ApiVO<Boolean> apiVO = ApiVO.error(Info.AUTH_EXPIRES.getCode(), Info.AUTH_EXPIRES.getMessage());
		response.setStatus(401);
		outputStream.write(JSON.toJSONBytes(apiVO));
		outputStream.flush();
		outputStream.close();
	}
}
